﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace scroll_ad
{
    public enum MouseAction
    {
        ResizeHorizontal_Left,
        ResizeHorizontal_Right,
        ResizeVertical_Top,
        ResizeVertical_Bottom,
        ResizeDeclining,
        Move,
        None,
    }
  
    public partial class AD : Form
    {
        private MouseAction action = MouseAction.None;
        private Point preMousePosition;
        private AdCfg adCfg = new AdCfg();
        private SyncInfo info = null;
        public AD(ref MyDelegate myEvent, ref RunScroll runScroll)
        {
            InitializeComponent();
            myEvent += new MyDelegate(this.onUpdateStyle);
            runScroll += new RunScroll(this.onRunScroll);
        }
        private void onRunScroll(bool enabled)
        {
            if (enabled)
            {
                this.timer1.Enabled = true;
            }
            else
            {
                this.timer1.Enabled = false;
            }
        }
        private Point offset;
        public void onUpdateStyle(SyncInfo info)
        {
            this.info = info;
            this.adString.Text = info.text;
            this.adString.BackColor = info.fontBgColor;
            this.adString.ForeColor = info.fontColor;
            this.BackColor = info.adBg;
            this.adString.Font = info.font;
        }
        private int border = 10;
        private MouseAction updateMouse(MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            if (border <= x && x <= this.Width - border &&
                border <= y && y <= this.Height - border)
            {
                // 移动窗口位置
                this.Cursor = Cursors.Hand;
                return MouseAction.Move;
            }
            else
            {
                // 修改窗口尺寸
                if (0 <= x && x <= border && border <= y && y <= this.Height - border)
                {
                    // 窗口左边
                    this.Cursor = Cursors.SizeWE;
                    return MouseAction.ResizeHorizontal_Left;
                }
                else if (this.Width - border <= x && x <= this.Width && border <= y && y <= this.Height - border)
                {
                    // 窗口右边
                    this.Cursor = Cursors.SizeWE;
                    return MouseAction.ResizeHorizontal_Right;
                }
                else if (0 <= y && y <= border && border <= x && x <= this.Width - border)
                {
                    // 窗口上边
                    this.Cursor = Cursors.SizeNS;
                    return MouseAction.ResizeVertical_Top;
                }
                else if (this.Height - border <= y && y < this.Height && border <= x && x <= this.Width - border)
                {
                    // 窗口下边
                    this.Cursor = Cursors.SizeNS;
                    return MouseAction.ResizeVertical_Bottom;
                }
                else
                {
                    return MouseAction.None;

                }
            }
            return MouseAction.None;
        }
        private void AD_MouseDown(object sender, MouseEventArgs e)
        {
            this.action = this.updateMouse(e);
            if (e.Button == MouseButtons.Left)
            {
                if (this.action == MouseAction.Move)
                {
                    Point cur = this.PointToScreen(e.Location);
                    this.offset = new Point(cur.X - this.Left, cur.Y - this.Top);
                }
                else if (this.action == MouseAction.ResizeHorizontal_Left ||
                    this.action == MouseAction.ResizeVertical_Top ||
                    this.action == MouseAction.ResizeHorizontal_Right ||
                    this.action == MouseAction.ResizeVertical_Bottom
                    )
                {
                    preMousePosition = MousePosition;
                }
            }
        }

        private void AD_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                if (this.action == MouseAction.Move)
                {
                    this.Location = new Point(MousePosition.X - this.offset.X, MousePosition.Y - offset.Y);
                }
                else if (this.action == MouseAction.ResizeHorizontal_Right)
                {
                    int diffX = MousePosition.X - preMousePosition.X;
                    this.Width += diffX;

                }
                else if (this.action == MouseAction.ResizeVertical_Bottom)
                {
                    int diffY = MousePosition.Y - preMousePosition.Y;
                    this.Height += diffY;
                }else if (this.action == MouseAction.ResizeVertical_Top)
                {
                    int diffY = MousePosition.Y - preMousePosition.Y;
                    this.Height -= diffY;
                    this.Location = new Point(this.Location.X, this.Location.Y + diffY);
                }else if (this.action== MouseAction.ResizeHorizontal_Left)
                {
                    int diffX = MousePosition.X - preMousePosition.X;
                    this.Width -= diffX;
                    this.Location = new Point(this.Location.X + diffX, this.Location.Y );
                }
                preMousePosition = MousePosition;
                this.adCfg.pos.X = this.Location.X;
                this.adCfg.pos.Y = this.Location.Y;
                this.adCfg.size.Width = this.Width;
                this.adCfg.size.Height = this.Height;
                this.adCfg.saveCfg();
            }
            else
            {
                this.updateMouse(e);
            }
        }

        private void AD_MouseUp(object sender, MouseEventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            Point curPos = this.adString.Location;

            if (this.info.speed > 0)
            {
                // 向右滚动完毕
                if (curPos.X > this.Width)
                {
                    this.adString.Location = new Point(-this.adString.Width, curPos.Y);
                }
            }
            else
            {
                // 向左滚动完毕
                if (curPos.X < -this.adString.Width)
                {
                    this.adString.Location = new Point(this.Width, curPos.Y);
                }
            }

            this.adString.Left += info.speed;
        }

        private void adString_Click(object sender, EventArgs e)
        {

        }


        private Point labelLocation;
        private Point mouseLocation;
        private void adString_MouseDown(object sender, MouseEventArgs e)
        {
            // 只有不移动的时候才能调整
            if (!this.timer1.Enabled && e.Button == MouseButtons.Left)
            {
                mouseLocation = MousePosition;
                labelLocation = this.adString.Location;
            }
        }

        private void adString_MouseMove(object sender, MouseEventArgs e)
        {
            // 只有不移动的时候才能调整
            if (!this.timer1.Enabled )
            {
                this.Cursor = Cursors.SizeAll;
                if(e.Button == MouseButtons.Left)
                {
                    int diffX = MousePosition.X - mouseLocation.X;
                    int diffY = MousePosition.Y - mouseLocation.Y;
                    this.adString.Location = new Point(labelLocation.X + diffX, labelLocation.Y + diffY);
                    this.adCfg.lableY = this.adString.Location.Y;
                    this.adCfg.saveCfg();
                }
            }
        }

        private void AD_Load(object sender, EventArgs e)
        {
            this.adCfg.readCfg();

            this.Location = this.adCfg.pos;
            this.Width = this.adCfg.size.Width;
            this.Height = this.adCfg.size.Height;
            this.adString.Location = new Point( this.adString.Location.X, this.adCfg.lableY);
        }
    }

    public class AdCfg
    {
        public Point pos;
        public Size size;
        public int lableY;

        public void readCfg()
        {
            this.pos = new Point(Properties.Settings.Default.AdWinPosX, Properties.Settings.Default.AdWinPosY);
            this.size = new Size(Properties.Settings.Default.AdWinWidth, Properties.Settings.Default.AdWinHeight);
            this.lableY = Properties.Settings.Default.AdLabelPosY;
        }
        public void saveCfg()
        {
            Properties.Settings.Default.AdLabelPosY = this.lableY;
            Properties.Settings.Default.AdWinPosX = this.pos.X;
            Properties.Settings.Default.AdWinPosY = this.pos.Y;
            Properties.Settings.Default.AdWinWidth = this.size.Width;
            Properties.Settings.Default.AdWinHeight = this.size.Height;
            Properties.Settings.Default.Save();
        }
    }
}
